#include "PreHeader.h"
#include "DataSL.h"
#include "GlobalData.h"

void DataSL::load()
{
    gHighScore[0] = MAX(0, getHighestScoreOne());
    gHighScore[1] = MAX(0, getHighestScoreTwo());
    gTestLevel = MAX(0, getCurStage());

    for (int i=0; i<10; i++)
    {
        gLevelDetail[i].isClear = getLevelDetail(i);
    }
    
    CCUserDefault* ud = CCUserDefault::sharedUserDefault();
    gIsReviewd = ud->getBoolForKey("isReviewed");
    gCurReviewCount = ud->getIntegerForKey("CurReviewCount", 3);
}

void DataSL::save()
{
    setHighestScoreOne(gHighScore[0]);
    setHighestScoreTwo(gHighScore[1]);
    setCurStage(gTestLevel);

    for (int i=0; i<10; i++)
    {
        setLevelDetail(i, gLevelDetail[i].isClear);
    }

    CCUserDefault* ud = CCUserDefault::sharedUserDefault();
  
    ud->setBoolForKey("isReviewed", gIsReviewd);
    ud->setIntegerForKey("CurReviewCount", gCurReviewCount);

    CCUserDefault::sharedUserDefault()->flush();
}

bool DataSL::setHighestScoreOne(int score)
{
    bool result = true;

    int oldScore = _getIntData("highestScoreOne");
    if (score > oldScore)
    {
        result = _setIntData("highestScoreOne", score);
    }

    return result;
}

bool DataSL::setHighestScoreTwo(int score)
{
    bool result = true;

    int oldScore = _getIntData("highestScoreTwo");
    if (score > oldScore)
    {
        result = _setIntData("highestScoreTwo", score);
    }

    return result;
}

bool DataSL::setHighestStage(int stage)
{
    bool result = false;

    int oldStage = _getIntData("highestStage");
    if (stage > oldStage)
    {
        result = _setIntData("highestStage", stage);
    }

    return result;
}

bool DataSL::setCurStage(int stage)
{
    bool result = false;

    result = _setIntData("curStage", stage);

    return result;
}

bool DataSL::setLevelDetail(int level, bool state)
{
    bool result = false;

    char key[128];
    memset(key, 0, sizeof(char)*128);

    sprintf(key, "levelDetail%d", level);

    result = _setBoolData(key, state);

    return result;
}

int DataSL::getHighestScoreOne()
{
    int score = 0;

    score = _getIntData("highestScoreOne");

    return score;
}

int DataSL::getHighestScoreTwo()
{
    int score = 0;

    score = _getIntData("highestScoreTwo");

    return score;
}

int DataSL::getHighestStage()
{
    int stage = 0;

    stage = _getIntData("highestStage");

    return stage;
}

int DataSL::getCurStage()
{
    int stage = 0;

    stage = _getIntData("curStage");

    return stage;
}

bool DataSL::getLevelDetail(int level)
{
    bool state = false;

    char key[128];
    memset(key, 0, sizeof(char)*128);

    sprintf(key, "levelDetail%d", level);

    state = _getBoolData(key);

    return state;
}

bool DataSL::_setStringData(std::string key, std::string data)
{
    bool result = false;

    if (key.size() > 0)
    {
        CCUserDefault* ud = CCUserDefault::sharedUserDefault();
        ud->setStringForKey(key.c_str(), data);

        result = true;
    }

    return result;
}

bool DataSL::_setBoolData(std::string key, bool data)
{
    bool result = false;

    if (key.size() > 0)
    {
        CCUserDefault* ud = CCUserDefault::sharedUserDefault();
        ud->setBoolForKey(key.c_str(), data);

        result = true;
    }

    return result;
}

bool DataSL::_setIntData(std::string key, int data)
{
    bool result = false;

    if (key.size() > 0)
    {
        CCUserDefault* ud = CCUserDefault::sharedUserDefault();
        ud->setIntegerForKey(key.c_str(), data);

        result = true;
    }

    return result;
}

std::string DataSL::_getStringData(std::string key)
{
    std::string data;

    CCUserDefault* ud = CCUserDefault::sharedUserDefault();
    data = ud->getStringForKey(key.c_str(), "");

    return data;
}

bool DataSL::_getBoolData(std::string key)
{
    bool data;

    CCUserDefault* ud = CCUserDefault::sharedUserDefault();
    data = ud->getBoolForKey(key.c_str(), false);

    return data;
}

int DataSL::_getIntData(std::string key)
{
    int data = -1;

    CCUserDefault* ud = CCUserDefault::sharedUserDefault();
    data = ud->getIntegerForKey(key.c_str(), -1);

    return data;
}
